/**
 * @Project: Runer
 * @Title: SysBroadcastReceiver.java
 * @Package com.runlooker.runer.Broadcast
 * @author YINENG
 * @date 2014-7-8 上午9:13:46
 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved.
 * @version V1.0
 */
package com.runlooker.runer.broadcast;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.interfaces.FileSendStantusListener;
import com.runlooker.runer.interfaces.NetEventHandlerListener;
import com.runlooker.runer.service.XmppService;
import com.runlooker.runer.util.L;

/**
 * 
 * 接收网络变化，开机、关机的广播
 * 
 * 
 * @ClassName SysBroadcastReceiver
 * @Description 模块描述
 * @author town
 * @Date 2014-7-8
 */
public class SysBroadcastReceiver extends BroadcastReceiver {
	private final String tag = SysBroadcastReceiver.class.getSimpleName();
	private static ArrayList<NetEventHandlerListener> mNetEventHandlerListeners = new ArrayList<NetEventHandlerListener>();

	public static void addNetworkListener(NetEventHandlerListener fc) {
		if (mNetEventHandlerListeners.contains(fc)) {
			mNetEventHandlerListeners.remove(fc);
		}
		mNetEventHandlerListeners.add(fc);
	}

	public static void removeNetworkListener(NetEventHandlerListener fc) {
		mNetEventHandlerListeners.remove(fc);
	}

	public static void removeAllNetworkListener() {
		mNetEventHandlerListeners.clear();
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (TextUtils.equals(action, ConnectivityManager.CONNECTIVITY_ACTION)) { // 网络变化发出的广播
			if (mNetEventHandlerListeners.size() > 0) {
				for (NetEventHandlerListener handler : mNetEventHandlerListeners) {
					handler.onNetChange();
				}
			}
		}
		if (MyApplication.isServiceRunning(context, "XmppService")) {
			Intent intent1 = new Intent(context, XmppService.class);
			context.startService(intent1);
		}
	}
}
